package com.tpyule.tpclass.web.Controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tpyule.tpclass.entity.bean.FailResultBean;
import com.tpyule.tpclass.entity.bean.ResultBean;
import com.tpyule.tpclass.entity.bean.SuccessResultBean;
import com.tpyule.tpclass.entity.dto.CommentQueryDto;
import com.tpyule.tpclass.entity.pojo.Chapter;
import com.tpyule.tpclass.entity.pojo.Comment;
import com.tpyule.tpclass.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author zzz
 * @version 1.0
 * @date 2020/4/15 17:14
 */
@RestController
@RequestMapping("/comment")
public class CommentController extends BaseController{
    @Autowired
    CommentService commentService;
    @RequestMapping("/addComment")
    @ResponseBody
    public void addComment(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ResultBean resultBean = null;
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            String commentData = request.getParameter("commentData");
            if(commentData!=null){
                Comment comment = objectMapper.readValue(commentData,Comment.class);
                resultBean = commentService.addComment(comment);
            }else {
                resultBean = new FailResultBean("数据错误");
            }
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        } finally {
            this.setAjaxResponseResult(response,resultBean);
        }
    }

    @RequestMapping("/getListComment")
    @ResponseBody
    public void getListComment(HttpServletRequest request, HttpServletResponse response) throws IOException {
        /**
         * 根据前台传值判断如何取出评论
         * 1：只传courseId则取出该课程的所有评论
         * 2：传courseId\sectionId 或者 sectionId 取出节下的所有评论
         * 3： 传courseId+userId 传在课程下某一用户的评论
         *      传section+userId 传在节下某一用户的评论
         * 4：传userId 取出某用户的所有评论
         */
        ResultBean resultBean = null;
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            String commentQueryData = request.getParameter("commentQueryData");
            if(commentQueryData!=null){
                CommentQueryDto comment = objectMapper.readValue(commentQueryData, CommentQueryDto.class);
                resultBean = commentService.getListComment(comment);
            }else {
                resultBean = new FailResultBean("数据错误");
            }
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        } finally {
            this.setAjaxResponseResult(response,resultBean);
        }
    }

}
